

import 'dart:io';

import 'package:awesome_project/utils/ext/log_extensions.dart';
import 'package:dio/dio.dart';
import 'package:dio/io.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

import '../local/constants.dart';
import 'custom_Interceptors.dart';
import 'my_background_transformer.dart';

final dioProvider = Provider((_) => AppDio.getInstance());

class AppDio {
  static BaseOptions get dioBaseOptions {
    const duration = Duration(seconds: 30);
    return BaseOptions(
      baseUrl: Constants.endpoint,
      contentType: 'application/json',
      connectTimeout: duration,
      sendTimeout: duration,
      receiveTimeout: duration,
      receiveDataWhenStatusError: true,
    );
  }

  static List<Interceptor> get dioInterceptors =>
      <Interceptor>[CustomInterceptors()];

  static final Dio dio = Dio()
    ..options = dioBaseOptions
    ..httpClientAdapter= IOHttpClientAdapter(
        validateCertificate: (X509Certificate? certificate, String host, int port) {
          return true;
        })
    ..transformer = MyBackgroundTransformer()
    ..interceptors.addAll(dioInterceptors);

  static Dio getInstance() => AppDio.dio;
}
